﻿Imports System.Data
Imports System.Data.OleDb
Imports System.Web.Security

Public Class News
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim OleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        OleDbConn.Open()
        Dim value As String
        value = Convert.ToString(Context.Request.QueryString("ID"))
        Dim SqlString As String = "SELECT ID,Title,Story,Submitted, SubmissionDate FROM NewsItems WHERE ID=" & value
        Dim postsCmd As New OleDbCommand(SqlString, OleDbConn)
        Dim commentsCmd As New OleDbCommand("SELECT ID, commentMadeTitle, commentMade, PostedID, news_FKID FROM newsComments WHERE news_FKID IN (SELECT ID FROM NewsItems)", OleDbConn)
        Dim ds As New DataSet()
        Dim oleDbAdapter As New OleDbDataAdapter(postsCmd)
        oleDbAdapter.Fill(ds, "FirstTable")
        oleDbAdapter.SelectCommand = commentsCmd
        oleDbAdapter.Fill(ds, "SecondTable")
        oleDbAdapter.Dispose()
        OleDbConn.Close()
        ds.Relations.Add(New DataRelation("PostingsID", ds.Tables(0).Columns("ID"), ds.Tables(1).Columns("news_FKID"), False))

        ListView1.DataSource = ds.Tables(0)
        ListView1.DataBind()

    End Sub

    Protected Sub doBinding(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewItemEventArgs)
        Dim Item As ListViewItem = e.Item
        If (Item.ItemType = ListViewItemType.DataItem) Then
            Dim commentRepeater As Repeater = Item.FindControl("Repeater1")
            Dim drv As DataRowView = Item.DataItem
            commentRepeater.DataSource = drv.CreateChildView("PostingsID")
            commentRepeater.DataBind()
        End If

    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        Dim SqlString As String = "Insert into newsComments(commentMadeTitle,commentMade,PostedID,news_FKID) Values (@f1,@f2,@f3,@f4)"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
      
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", TextBox1.Text)
        cmd.Parameters.AddWithValue("@f2", TextBox2.Text)
        cmd.Parameters.AddWithValue("@f3", User.Identity.Name)
        cmd.Parameters.AddWithValue("@f4", Request.Params("ID"))
        oleDbConn.Open()
        cmd.ExecuteNonQuery()
        Response.Redirect("thanks.aspx")
    End Sub
End Class